package cn.fintecher.pangolin.service.manage.web;

import cn.fintecher.pangolin.common.web.BaseController;
import cn.fintecher.pangolin.service.manage.request.AddPrincipalRequest;
import cn.fintecher.pangolin.service.manage.request.FindPrincipalRequest;
import cn.fintecher.pangolin.service.manage.request.ModifyPrincipalRequest;
import cn.fintecher.pangolin.service.manage.response.PrincipalResponse;
import cn.fintecher.pangolin.service.manage.service.PrincipalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Auther: xiaqun
 * @Description: 委托方相关接口
 * @Date: 10:25 2019/8/15
 */

@RestController
@RequestMapping("/api/principalController")
@Api(value = "委托方相关接口", description = "委托方相关接口")
public class PrincipalController extends BaseController {
    @Autowired
    private PrincipalService principalService;

    /**
     * 新增委托方
     */
    @ApiOperation(value = "新增委托方", notes = "新增委托方")
    @PostMapping("/addPrincipal")
    public ResponseEntity<Void> addPrincipal(@Valid @RequestBody AddPrincipalRequest request,
                                             @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to add principal,{}", request);
        principalService.addPrincipal(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改委托方
     */
    @ApiOperation(value = "修改委托方", notes = "修改委托方")
    @PutMapping("/modifyPrincipal")
    public ResponseEntity<Void> modifyPrincipal(@Valid @RequestBody ModifyPrincipalRequest request,
                                                @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to modify principal,{}", request);
        principalService.modifyPrincipal(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 多条件查询委托方
     */
    @ApiOperation(value = "多条件查询委托方", notes = "多条件查询委托方")
    @GetMapping("/findAllPrincipal")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<PrincipalResponse>> findAllPrincipal(FindPrincipalRequest request, Pageable pageable,
                                                                    @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to find all principal,{}", request);
        Page<PrincipalResponse> page = principalService.findAllPrincipal(request, pageable, token);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 查询全部委托方
     */
    @ApiOperation(value = "查询全部委托方", notes = "查询全部委托方")
    @GetMapping("/findAllPrincipalNoPage")
    public ResponseEntity<List<PrincipalResponse>> findAllPrincipalNoPage(@RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to find all principal no page");
        List<PrincipalResponse> list = principalService.findAllPrincipalNoPage(token);
        return ResponseEntity.ok().body(list);
    }
}
